package com.example.broadcastbesrpractice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private ForceOfflineReceiver2 mReceiver;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button forceOffline = (Button) findViewById(R.id.force_offline);
        forceOffline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: start");
                Intent intent = new Intent("com.example.broadcastbesrpractice.FORCE_OFFLINE");
                Log.d(TAG, "Intent=" + intent.toString());
                sendBroadcast(intent);
                Log.d(TAG, "onClick: end");
            }
        });

        Log.d(TAG, "onCreate: start");
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.broadcastbesrpractice.FORCE_OFFLINE");
        Log.d(TAG, "intentFilter=" + intentFilter.toString());
        mReceiver = new ForceOfflineReceiver2();
        registerReceiver(mReceiver, intentFilter);
        Log.d(TAG, "onCreate: end");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }
}